package hongcaosp.app.android.user.dynamic;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import xlj.lib.android.base.view.recyclepager.FixLinearSnapHelper;
import xlj.lib.android.base.view.recyclepager.OnItemVisibleListener;

public class VisibleItemLinearLayoutManager extends LinearLayoutManager {
    private OnItemVisibleListener mOnItemVisibleListener;
//    private LinearSnapHelper linearSnapHelper;
    private RecyclerView mRecyclerView;
    private int mDrift;//位移，用来判断移动方向

    public VisibleItemLinearLayoutManager(Context context, int orientation) {
        super(context, orientation, false);

        init();
    }

    public VisibleItemLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
        init();
    }

    private void init() {
//        linearSnapHelper = new FixLinearSnapHelper();
    }

    @Override
    public void onAttachedToWindow(RecyclerView view) {
        super.onAttachedToWindow(view);
        this.mRecyclerView = view;
//        linearSnapHelper.attachToRecyclerView(view);
        mRecyclerView.addOnChildAttachStateChangeListener(mChildAttachStateChangeListener);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        super.onLayoutChildren(recycler, state);
    }

    /**
     * 滑动状态的改变
     * 缓慢拖拽-> SCROLL_STATE_DRAGGING
     * 快速滚动-> SCROLL_STATE_SETTLING
     * 空闲状态-> SCROLL_STATE_IDLE
     *
     * @param state
     */
    @Override
    public void onScrollStateChanged(int state) {
        switch (state) {
            case RecyclerView.SCROLL_STATE_IDLE:
//                View viewIdle = linearSnapHelper.findSnapView(this);
//                if (viewIdle != null) {
//                    int positionIdle = getPosition(viewIdle);
//                    if (mOnItemVisibleListener != null) {
//                        mOnItemVisibleListener.onItemSelected(positionIdle, positionIdle == getItemCount() - 1, viewIdle);
//                    }
//                }
                break;
            case RecyclerView.SCROLL_STATE_DRAGGING:

                break;
            case RecyclerView.SCROLL_STATE_SETTLING:

                break;
        }
    }

    /**
     * 监听竖直方向的相对偏移量
     *
     * @param dy
     * @param recycler
     * @param state
     * @return
     */
    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        this.mDrift = dy;
        return super.scrollVerticallyBy(dy, recycler, state);
    }

    /**
     * 监听水平方向的相对偏移量
     *
     * @param dx
     * @param recycler
     * @param state
     * @return
     */
    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        this.mDrift = dx;
        return super.scrollHorizontallyBy(dx, recycler, state);
    }

    /**
     * 设置监听
     *
     * @param listener
     */
    public void setOnViewPagerListener(OnItemVisibleListener listener) {
        this.mOnItemVisibleListener = listener;
    }

    private RecyclerView.OnChildAttachStateChangeListener mChildAttachStateChangeListener = new RecyclerView.OnChildAttachStateChangeListener() {
        /**
         * itemView依赖Window
         */
        @Override
        public void onChildViewAttachedToWindow(View view) {
            if (mOnItemVisibleListener != null && getChildCount() == 1) {
                mOnItemVisibleListener.onItemInit(getPosition(view), view);
            }
        }

        /**
         *itemView脱离Window
         */
        @Override
        public void onChildViewDetachedFromWindow(View view) {
            if (mDrift >= 0) {
                if (mOnItemVisibleListener != null)
                    mOnItemVisibleListener.onItemRelease(true, getPosition(view), view);
            } else {
                if (mOnItemVisibleListener != null)
                    mOnItemVisibleListener.onItemRelease(false, getPosition(view), view);
            }

        }
    };
}
